//
//  LGSmokingRecordController.m
//  XieHeHeartDisease
//
//  Created by 1234 on 2024/1/31.
//  康复记录 -- 戒烟

#import "LGSmokingRecordController.h"
#import "LGMonitoringHistoryView.h"
#import "LGSmokeProgressView.h"
#import "LGSmokeRecordHeader.h"
#import "LGSmokeRecordCell.h"
#import "LGPickerView.h"
@interface LGSmokingRecordController ()<UITableViewDelegate,UITableViewDataSource>
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UIView *footerView;
/**   */
@property (nonatomic , strong)LGMonitoringHistoryView *historyView;
/**   */
@property (nonatomic , strong)LGSmokeProgressView *headView;
/**   */
@property (nonatomic , copy)NSString *monthString;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
/**   */
@property (nonatomic , strong)NSDateFormatter *formatter1;
/**   */
@property (nonatomic , strong)NSDateFormatter *formatter2;
/**   */
@property (nonatomic , assign)BOOL firstLoad;

@end

@implementation LGSmokingRecordController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.firstLoad = YES;
    self.view.backgroundColor = [UIColor colorWithString:@"#F3F7F8"];
    self.monthString = [self.formatter2 stringFromDate:[NSDate date]];
    [self.view addSubview:self.tableView];
    [self emptyViewWithSuperView:self.tableView frame:self.tableView.bounds];
    self.emptyView.backgroundColor = [UIColor colorWithString:@"#F3F7F8"];
    //285
    [self viewWillAppearAction];
}

//模块被选中
-(void)viewWillAppearAction{
    if(self.firstLoad == YES){
        [self requestData];
        self.firstLoad = NO;
    }
   
    if (self.dataArray.count == 0) {
        [self requestData];
    }
}

//请求历史记录
-(void)requestData{
    [RequestUtil withPOST:@"/doctor/patient/recordList" parameters:@{@"month":self.monthString,@"patient_id":self.patientid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(code == 200){
            if(([returnData[@"progress"] isKindOfClass:[NSDictionary class]] && [[returnData[@"progress"] allKeys] count]>0)){
                self.emptyView.frame = CGRectMake(0, viewPix(285), Screen_W, self.tableView.bounds.size.height-viewPix(285));
                self.headView.dataDic = returnData[@"progress"];
               
            }else{
                self.emptyView.frame = self.tableView.bounds;
            }
            self.dataArray = returnData[@"records"];
            [self.tableView reloadData];
        }
        if(self.dataArray.count == 0){
            self.footerView.frame = CGRectMake(0, 0, Screen_W, viewPix(300));
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无记录" offset:viewPix(60)];
        }else{
            self.footerView.frame = CGRectMake(0, 0, Screen_W, 0.001);
        }
        self.tableView.tableFooterView = self.footerView;
    } error:^(NSError *error) {
        self.isfirst = NO;
        if(self.dataArray.count == 0){
            self.footerView.frame = CGRectMake(0, 0, Screen_W, viewPix(300));
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无记录" offset:viewPix(60)];
        }else{
            self.footerView.frame = CGRectMake(0, 0, Screen_W, 0.001);
        }
        self.tableView.tableFooterView = self.footerView;
    }];
}

//选择月份
-(void)showMonthPickerView{
    NSMutableArray *yearArray = [NSMutableArray array];
    NSArray *monthArray = @[@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12"];
    for (NSInteger i=2020; i<2100; i++) {
        [yearArray addObject:[NSString stringWithFormat:@"%ld",i]];
    }
    LGPickerView *pickerView = [[LGPickerView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    pickerView.pickArry = @[yearArray,monthArray];
    pickerView.units = @[@"年",@"月"];
    pickerView.selectStr = [self.monthString stringByReplacingOccurrencesOfString:@"-" withString:@"|"];
    [self.view.window addSubview:pickerView];
    __weak typeof(self) weakSelf = self;
    pickerView.selectPickViewResult = ^(NSArray *resultArray) {
        weakSelf.historyView.monthLabel.text = [NSString stringWithFormat:@"%@年%@月",resultArray[0],resultArray[1]];
        weakSelf.monthString = [NSString stringWithFormat:@"%@-%@",resultArray[0],resultArray[1]];
        [weakSelf requestData];
    };
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count>0?1:0;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGSmokeRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dataDic = self.dataArray[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return viewPix(40);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LGSmokeRecordHeader *headView = [[LGSmokeRecordHeader alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(40))];
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return viewPix(15);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    LGCornerFooterView *footView = [[LGCornerFooterView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(15))];
    footView.backgroundColor = [UIColor clearColor];
    footView.bottomMargin = viewPix(10);
    CGFloat dateWidth = viewPix(87);
    CGFloat itemWidth = (Screen_W-viewPix(32)-dateWidth)/2.0;
    for (NSInteger i=0; i<3; i++) {
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake((viewPix(16)+dateWidth)+itemWidth*i, 0, 1, viewPix(5))];
        lineView.backgroundColor = [UIColor colorWithString:@"#F2F2F2"];
        [footView addSubview:lineView];
    }
    return footView;
}

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.tableView.frame = CGRectMake(0, 0, Screen_W, self.view.bounds.size.height);
}
#pragma mark
#pragma mark ====> 创建控件
-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, self.view.bounds.size.height) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGSmokeRecordCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor colorWithString:@"clearColor"];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.rowHeight = viewPix(35);
        _tableView.dataSource = self;
        _tableView.delegate = self;
        UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(285))];
        [headerView addSubview:self.headView];
        [headerView addSubview:self.historyView];
        _tableView.tableHeaderView = headerView;
        _tableView.tableFooterView = self.footerView;
    }
    return _tableView;
}
-(LGMonitoringHistoryView *)historyView{
    if(!_historyView){
        _historyView = [[LGMonitoringHistoryView alloc] initWithFrame:CGRectMake(0, kMaxY(self.headView.frame)+viewPix(10), Screen_W, viewPix(70))];
        _historyView.monthLabel.text = [self.formatter1 stringFromDate:[NSDate date]];
        [_historyView.monthBtn addTarget:self action:@selector(showMonthPickerView) forControlEvents:UIControlEventTouchUpInside];
    }
    return _historyView;
}

-(LGSmokeProgressView *)headView{
    if(!_headView){
        _headView = [[LGSmokeProgressView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(212))];
    }
    return _headView;
}

-(UIView *)footerView{
    if(!_footerView){
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(300))];
    }
    return _footerView;
}

-(NSDateFormatter *)formatter1{
    if(!_formatter1){
        _formatter1 = [[NSDateFormatter alloc] init];
        _formatter1.dateFormat = @"YYYY年MM月";
    }
    return _formatter1;
}

-(NSDateFormatter *)formatter2{
    if(!_formatter2){
        _formatter2 = [[NSDateFormatter alloc] init];
        _formatter2.dateFormat = @"YYYY-MM";
    }
    return _formatter2;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
